﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Linq;
using System.Diagnostics;
using System.Threading.Tasks;
using LucyGrid;

namespace LucyGridTest
{
	/// <summary>
	/// Summary description for LucyGTest
	/// </summary>
	[TestClass]
	public class LucyGTest
	{
		public LucyGTest()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		//
		// You can use the following additional attributes as you write your tests:
		//
		// Use ClassInitialize to run code before running the first test in the class
		// [ClassInitialize()]
		// public static void MyClassInitialize(TestContext testContext) { }
		//
		// Use ClassCleanup to run code after all tests in a class have run
		// [ClassCleanup()]
		// public static void MyClassCleanup() { }
		//
		// Use TestInitialize to run code before running each test 
		// [TestInitialize()]
		// public void MyTestInitialize() { }
		//
		// Use TestCleanup to run code after each test has run
		// [TestCleanup()]
		// public void MyTestCleanup() { }
		//
		#endregion


		string GetLucyGPath() {
			return Directory.GetFiles(Path.GetFullPath(TestContext
				.TestRunDirectory + "../../../LucyG/bin/debug")).First(f => f.EndsWith(".exe"));
		}

		Task[] StartGridNode(int nodes)
		{
			return Enumerable.Range(0, nodes)
				.Select(i =>
						Task.Run(() =>
						{
							var file = Directory.GetFiles(Path.GetFullPath(TestContext
									.TestRunDirectory + "../../../LucyGridNode/bin/debug")).First(f => f.EndsWith(".exe"));
							var p = Process.Start(new ProcessStartInfo()
							{
								FileName = file
							});
							p.WaitForExit();
						})).ToArray();
		}

		/*[TestMethod]
		public void LocalCommandTest()
		{
			var nodes = StartGridNode(1);
			var args = "-i 2 -nc \"cmd echo %COMPUTERNAME% \"";
			var p = Process.Start(new ProcessStartInfo()
			{
				FileName = GetLucyGPath(),
				Arguments = args,
				RedirectStandardOutput = true,
				RedirectStandardInput = true,
				UseShellExecute = false
			});
			p.StandardInput.WriteLine(string.Empty);
			p.WaitForExit();
			// Close nodes
			new GridComm().EmitStopAll();
			Task.WaitAll(nodes);
		}*/
	}
}
